package com.wc.算法提高课.C第三章_图论.最近公共祖先.距离;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/30 16:35
 * @description https://www.acwing.com/problem/content/1173/
 */
public class Main {
    /**
     * 思路：<p>
     * 离线tarjan求lca<p>
     * 基于向上询问法的优化, 做离线操作
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, M = N << 1, idx = 1;
    static int[] h = new int[N], e = new int[M], w = new int[M], ne = new int[M], dist = new int[N];
    // st[i] 表示当前点的状态， st[i] = 0 表示还未被访问过, st[i] = 1表示正在被访问, st[i] = 2表示已经访问过了
    static int[] st = new int[N], res = new int[M];
    static int[] p = new int[N];
    static ArrayList<Pair>[] query = new ArrayList[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i <= n; i++) query[i] = new ArrayList<>();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        for (int i = 1; i <= m; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            query[a].add(new Pair(b, i));
            query[b].add(new Pair(a, i));
        }

        dfs(1, -1);
        for (int i = 1; i <= n; i++) p[i] = i;
        tarjan(1);
        for (int i = 1; i <= m; i++) out.println(res[i]);
        out.flush();
    }

    static void tarjan(int u) {
        st[u] = 1;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (st[j] == 0) {
                tarjan(j);
                p[j] = u;
            }
        }
        for (Pair pair : query[u]) {
            int b = pair.x, id = pair.y;
            if (st[b] == 2) {
                res[id] = dist[u] + dist[b] - dist[find(b)] * 2;
            }
        }
        st[u] = 2;
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    static void dfs(int u, int fa) {
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            dist[j] = dist[u] + w[i];
            dfs(j, u);
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static class Pair implements Comparable<Pair> {
        int x, y;

        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Pair o) {
            if (x == o.x) {
                return y - o.y;
            }
            return x - o.x;
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}